import './App.css'
import React from 'react'
import { useMount } from 'ahooks'
import { createAlova } from 'alova'
import adapterFetch from 'alova/fetch'

const alovaInstance = createAlova({ requestAdapter: adapterFetch(), responded: response => response.json() })
const getBindOpenWeChatUnionId = `https://transport.ljkjkf.com/yunshudaping/BindOpenWeChatUnionId`

const commonUrl = 'https://common-api.ljkjkf.com/wechatProjects/'
const gjeqUrl = 'https://gjeq-rest.ljkjkf.com/api/users/'
const getCommon = 'BindOpenWeChatUnionId'
const getProject = 'GetProjectInfoByLogin'

const apiMapping = ['gjeq']

const App = () => {
    useMount(async () => {
        const params = new URLSearchParams(window.location.search)
        const code = params.get('code')
        const data = params.get('state')
        const [QRUuid, state, client_id, common] = data.split('@')
        if (client_id) {
            const url = apiMapping.includes(client_id) ? gjeqUrl : commonUrl
            if (common) {
                await alovaInstance.Post(url + getCommon, { code, state, QRUuid, clientid: client_id })
            } else {
                let urlTwo = ''
                const res = await alovaInstance.Get(`${url}${getProject}?login=${client_id}`)
                const { data } = res
                if (data) urlTwo = data.apiUrl + data.bindUnionIdUrl
                else urlTwo = res.apiUrl + res.bindUnionIdUrl
                await alovaInstance.Post(urlTwo, { code, state, QRUuid, clientid: client_id })
            }
        } else await alovaInstance.Post(getBindOpenWeChatUnionId, { code, state, QRUuid })
    })
    return (
        <div className='App'>
            <div className="spinner">
                <div className="spinner1" />
            </div>
        </div>
    )
}

export default App;